package cate.game.role.farm.spicefb.po;

import cate.common.table.farm.spicefb.row.SpiceFbChapterRow;
import cate.common.util.GameResult;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.farm.spicefb.msg.SpiceFbChapterResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "香料副本章节数据")
public class SpiceFbChapter extends RoleDependent {
    @NoteField(value = "章节id")
    public int chapter;

    @NoteField(value = "章节的星级数量")
    public int star;

    @NoteField(value = "通关数量")
    @JsonIgnore
    public int pass;

    @NoteField(value = "本章节的关卡信息", detail = "map<关卡id，关卡信息>")
    public Map<Integer, SpiceFbLevel> levelMap;

    @NoteField("章节奖励是否领取")
    public boolean rewardGot;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (levelMap == null) {
            levelMap = new HashMap<>();
        }
        levelMap.values().forEach(SpiceFbLevel::initialize);
    }

    @Override
    public void onEnter() {
        updateChapter();
    }

    public SpiceFbChapter() {
    }

    public SpiceFbChapter(int chapter) {
        this.chapter = chapter;
    }

    /**
     * 获取关卡信息
     */
    public SpiceFbLevel getLevel(int combatNum) {
        SpiceFbLevel level = levelMap.get(combatNum);
        if (level == null) {
            level = new SpiceFbLevel(combatNum);
            level.initialize();
        }
        levelMap.put(combatNum, level);
        return level;
    }

    /**
     * 更新章节信息
     */
    public void updateChapter() {
        star = 0;
        pass = 0;
        levelMap.values().forEach(
                a -> {
                    star += a.challenge.size();
                    if (a.pass) {
                        pass++;
                    }
                });
    }

    /**
     * 能否解锁下一个章节
     */
    public boolean unlockNextChapter() {
        updateChapter();
        // 首先判断所有关卡都通了
        int levelNum = role.getGame().table.spiceFb.level.getLevelNum(chapter);
        if (levelNum > pass) {
            return false;
        }
        SpiceFbChapterRow row = role.getGame().table.spiceFb.chapter.get(chapter);
        if (row != null && row.starRequired > star) {
            return false;
        }
        SpiceFbChapterRow nextRow = role.getGame().table.spiceFb.chapter.get(chapter + 1);
        if (nextRow != null && (nextRow.openday > role.getGame().getOpenDay()
                || nextRow.worldTreeLevel > role.getRes().worldTree.heroShare.shareLvl)) {
            return false;
        }
        return true;
    }

    @JsonIgnore
    public GameResult<Void> getChapterReward() {
        GameResult<Void> r = new GameResult<>();
        if (rewardGot) {
            return r.fail("奖励已领取");
        }
        int maxStar = role.getGame().table.spiceFb.level.getMaxStar(chapter);
        if (star != maxStar) {
            return r.fail("未达到领取条件");
        }
        SpiceFbChapterRow row = role.getGame().table.spiceFb.chapter.get(chapter);
        if (row == null) {
            return r.fail("未达到领取条件");
        }
        rewardGot = true;
        new MixResAdder()
                .setRes(row.chapterRewardStr)
                .setSendMailWhenFull(true)
                .setDialogReward(true)
                .exe(role);
        noticeUpdate();
        return r;
    }

    public void noticeUpdate() {
        role.sendNow(new SpiceFbChapterResp(this));
    }
}
